package com.tracesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tracesystem.commons.Result;
import com.tracesystem.controller.Info.AllPriorityPageInfo;
import com.tracesystem.controller.Info.AllTypePageInfo;
import com.tracesystem.controller.form.*;
import com.tracesystem.model.IssuePriority;
import com.tracesystem.dao.IssuePriorityDao;
import com.tracesystem.model.IssueSeverity;
import com.tracesystem.model.IssueType;
import com.tracesystem.service.IssuePriorityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tracesystem.util.WoniuCopyUtil;
import com.tracesystem.util.exception.IssuePriorityException;
import com.tracesystem.util.exception.IssueTypeException;
import com.tracesystem.util.exception.code.IssuePriorityExceptionCode;
import com.tracesystem.util.exception.code.IssueTypeExceptionCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Kevin Martin
 * @since 2022-03-23
 */
@Service
public class IssuePriorityServiceImpl extends ServiceImpl<IssuePriorityDao, IssuePriority> implements IssuePriorityService {

    /**
     * 缺陷优先级实现层对象
     */
    @Resource
    private IssuePriorityService issuePriorityService;
    /**
     * 缺陷优先级业务层对象
     */
    @Resource
    private IssuePriorityDao issuePriorityDao;

    /**
     * 修改优先级分类
     * @param renewalPriorityDescForm
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> updatePriorityDesc(RenewalPriorityDescForm renewalPriorityDescForm) {
        IssuePriority issuePriority = issuePriorityService.getOne(//查询类型是否存在
                new QueryWrapper<IssuePriority>().eq("PRIORITY_CODE", renewalPriorityDescForm.getPriorityCode()));
        if(null==issuePriority) {//这个优先级不是空才走下面
            throw new IssuePriorityException(IssuePriorityExceptionCode.IssuePriority_NOTEXIST_ERROR);
        }else {
            if(issuePriority.getPriorityDesc().equals(renewalPriorityDescForm.getPriorityDesc())) {
                throw new IssuePriorityException(IssuePriorityExceptionCode.IssuePriority_EXIST_ERROR);
            }else {
                boolean b = issuePriorityService.saveOrUpdate(WoniuCopyUtil.copyOne(renewalPriorityDescForm, IssuePriority.class));
                if(!b) return Result.getInstance().setMsg("缺陷优先级不存在,已经为您新增该优先级分类");
                return Result.getInstance().setMsg("修改优先级分类成功");
            }
        }
    }

    /**
     * 修改缺陷优先级状态
     * @param renewalPriorityModuleStateForm
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> updateModuleState(RenewalPriorityModuleStateForm renewalPriorityModuleStateForm) {
        IssuePriority issuePriority = issuePriorityService.getOne(//查询类型是否存在
                new QueryWrapper<IssuePriority>().eq("PRIORITY_CODE", renewalPriorityModuleStateForm.getPriorityCode()));
        if (issuePriority.getModuleState().equals("有效")){
            issuePriority.setModuleState("禁用");
        }else {
            issuePriority.setModuleState("有效");
        }
        int i = issuePriorityDao.updateById(WoniuCopyUtil.copyOne(issuePriority, IssuePriority.class));
        if(i>0) return Result.getInstance().setMsg("修改缺陷优先级状态成功");
        return Result.getInstance().setCode(50001).setMsg("修改缺陷优先级状态失败");
    }

    /**
     * 根据缺陷优先级关键字模糊查询
     * @param likePriorityListForm
     * @return
     */
    @Override
    public Result<AllPriorityPageInfo> findPagePriorityListByPriorityDesc(LikePriorityListForm likePriorityListForm) {
        QueryWrapper<IssuePriority> wrapper = new QueryWrapper<IssuePriority>().orderByDesc("PRIORITY_CODE");
        if(likePriorityListForm.getPriorityDesc() != null&& !likePriorityListForm.getPriorityDesc().trim().equals(""))
            wrapper.like("PRIORITY_DESC",likePriorityListForm.getPriorityDesc());

        Page<IssuePriority> wa = page(new Page<>(likePriorityListForm.getPageIndex(),likePriorityListForm.getPageSize()),wrapper);
        List<AllPriorityPageInfo> pageInfoList = WoniuCopyUtil.copyMany(wa.getRecords(), AllPriorityPageInfo.class);

        Page<AllPriorityPageInfo> page = new Page<>();
        page.setRecords(pageInfoList)
                .setTotal(wa.getTotal())
                .setCurrent(wa.getCurrent())
                .setSize(wa.getSize())
                .setPages(wa.getPages());
        return Result.getInstance(Page.class).setData(page).setMsg("查询成功");
    }

    /**
     * 查询全部优先级
     * @param findAllPriorityListForm
     * @return
     */
    @Override
    public Result<AllPriorityPageInfo> getAllPriorityList(FindAllPriorityListForm findAllPriorityListForm) {
        QueryWrapper<IssuePriority> wrapper = new QueryWrapper<IssuePriority>().orderByDesc("PRIORITY_CODE");

        Page<IssuePriority> wa = page(new Page<>(findAllPriorityListForm.getPageIndex(),findAllPriorityListForm.getPageSize()),wrapper);
        List<AllPriorityPageInfo> pageInfoList = WoniuCopyUtil.copyMany(wa.getRecords(), AllPriorityPageInfo.class);

        Page<AllPriorityPageInfo> page = new Page<>();
        page.setRecords(pageInfoList)
                .setTotal(wa.getTotal())
                .setCurrent(wa.getCurrent())
                .setSize(wa.getSize())
                .setPages(wa.getPages());
        return Result.getInstance(Page.class).setData(page).setMsg("查询成功");
    }

    //新增
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> insertPriority(AddPriorityForm addPriorityForm) {
        IssuePriority issuePriority = issuePriorityService.getOne(//查询类型是否存在
                new QueryWrapper<IssuePriority>().eq("PRIORITY_DESC", addPriorityForm.getPriorityDesc()));
        //存在则抛出已存在异常
        if(null!=issuePriority) throw new IssuePriorityException(IssuePriorityExceptionCode.IssuePriority_EXIST_ERROR);
//        addPriorityForm.setModuleState("0");//默认值为0
        issuePriorityService.save(WoniuCopyUtil.copyOne(addPriorityForm, IssuePriority.class));
        return Result.getInstance().setMsg("新增缺陷优先级成功");
    }

    //删除
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> delectPriority(RemovePriorityForm removePriorityForm) {
        boolean b = issuePriorityService.removeById(removePriorityForm.getPriorityCode());
        if(!b) throw new IssuePriorityException(IssuePriorityExceptionCode.IssuePriority_NOTEXIST_ERROR);
        return Result.getInstance().setMsg("删除缺陷优先级成功");
    }

    //删除多个
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> delectPriorityList(RemovePriorityListForm removePriorityListForm) {
        List<Integer> PriorityCodes = removePriorityListForm.getPriorityCodes();
        for (Integer PriorityCode : PriorityCodes) {
            boolean b = issuePriorityService.removeById(PriorityCode);
            if(!b) throw new IssuePriorityException(IssuePriorityExceptionCode.IssuePriority_NOTEXIST_ERROR);
            continue;
        }
        return Result.getInstance().setMsg("删除缺陷优先级成功");
    }
}
